package com.tyk.bookstore.back.product.controller;


import cn.dev33.satoken.annotation.SaCheckPermission;
import com.tyk.bookstore.back.common.core.excel.ExcelManager;
import com.tyk.bookstore.back.common.core.model.page.PageVo;
import com.tyk.bookstore.back.common.core.model.result.R;
import com.tyk.bookstore.back.common.core.model.type.BinVo;
import com.tyk.bookstore.back.common.core.model.type.SuggestionQuery;
import com.tyk.bookstore.back.product.manager.excel.manager.ExcelConstant;
import com.tyk.bookstore.back.product.manager.excel.model.AuthorExcelVo;
import com.tyk.bookstore.back.product.model.query.AuthorAddQuery;
import com.tyk.bookstore.back.product.model.query.AuthorPageQuery;
import com.tyk.bookstore.back.product.model.query.AuthorUpdateQuery;
import com.tyk.bookstore.back.product.model.vo.AuthorPageVo;
import com.tyk.bookstore.back.product.model.vo.AuthorSimVo;
import com.tyk.bookstore.back.product.model.vo.AuthorVo;
import com.tyk.bookstore.back.product.service.AuthorService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author tyk
 * @description 作者管理
 */
@Tag(name = "商品管理/作者管理", description = "作者管理")
@RequestMapping({"api/product/author"})
@RestController
@RequiredArgsConstructor
public class AuthorController {

    private final AuthorService authorService;
    private final ExcelManager<AuthorExcelVo> excelManager;


    @Operation(summary = "获取作者详情", description = "获取作者详情")
    @SaCheckPermission("product:author:get")
    @GetMapping({"{id}"})
    public R<AuthorVo> getById(
            @Schema(description = "作者ID") @NotNull @Min(1)
            @PathVariable Long id
    ) {
        return R.ok(authorService.getById(id));
    }

    @Operation(summary = "分页查询作者", description = "分页查询作者")
    @SaCheckPermission("product:author:page")
    @GetMapping({"page"})
    public R<AuthorPageVo> getPage(
            @Validated @ParameterObject AuthorPageQuery query,
            BindingResult br
    ) {
        return br.hasErrors() ? R.ok(PageVo.instance(AuthorPageVo.class)) : R.ok(authorService.getPage(query));
    }


    @Operation(summary = "作者搜索建议列表", description = "作者搜索建议列表")
    @SaCheckPermission("product:author:suggest")
    @GetMapping({"suggest"})
    public R<List<BinVo>> getSuggestionList(@Validated @ParameterObject SuggestionQuery query) {
        return R.ok(authorService.getSuggestionList(query));
    }


    @Operation(summary = "作者列表", description = "作者列表")
    @SaCheckPermission("product:author:list")
    @GetMapping({"list"})
    public R<List<AuthorSimVo>> getList() {
        return R.ok(authorService.getList());
    }


    @Operation(summary = "添加作者", description = "添加作者")
    @SaCheckPermission("product:author:add")
    @PostMapping
    public R<Object> add(
            @Validated @RequestBody AuthorAddQuery query
    ) {
        authorService.add(query);
        return R.ok();
    }


    @Operation(summary = "更新作者", description = "更新作者")
    @SaCheckPermission("product:author:update")
    @PutMapping
    public R<Object> updateById(
            @Validated @RequestBody AuthorUpdateQuery query
    ) {
        authorService.updateById(query);
        return R.ok();
    }


    @Operation(summary = "删除作者", description = "删除作者")
    @SaCheckPermission("product:author:delete")
    @DeleteMapping({"{id}"})
    public R<Object> deleteById(
            @Schema(description = "作者ID")
            @NotNull @Min(1)
            @PathVariable Long id
    ) {
        authorService.deleteById(id);
        return R.ok();
    }


    @Operation(summary = "批量删除作者", description = "批量删除作者")
    @SaCheckPermission("product:author:delete")
    @DeleteMapping
    public R<Object> deleteBatchById(
            @Schema(description = "作者ID列表") @NotNull @NotEmpty List<Long> ids
    ) {
        authorService.deleteBatchById(ids);
        return R.ok();
    }


    @Operation(summary = "导出Excel", description = "导出Excel")
    @SaCheckPermission("product:author:excel:download")
    @GetMapping("excel")
    public void downloadExcel(AuthorPageQuery query, HttpServletResponse resp) {
        excelManager.write(
                authorService.getAuthorExcelVoList(query),
                ExcelConstant.AUTHOR_FILE_NAME,
                ExcelConstant.AUTHOR_SHEET,
                resp
        );
    }


    @Operation(summary = "导入Excel", description = "导入Excel")
    @SaCheckPermission("product:author:excel:upload")
    @PostMapping("excel")
    public R<Object> uploadExcel(MultipartFile file) {
        excelManager.save(
                file,
                authorService::saveAuthorExcelVoList
        );
        return R.ok();
    }


    @Operation(summary = "下载Excel模板", description = "下载Excel模板")
//    @SaCheckPermission("product:author:excel:template")
    @GetMapping("excel/template")
    public void excelTemplate(HttpServletResponse resp) {
        excelManager.template(
                ExcelConstant.AUTHOR_TEMPLATE,
                List.of("名字", "描述"),
                resp
        );
    }

}
